package com.ryenlii.datatrans.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eyelake.iot.common.tsdb.base.service.IInfluxBaseService;
import com.ryenlii.datatrans.common.api.ResponseCodeI18n;
import com.ryenlii.datatrans.core.Result;
import com.ryenlii.datatrans.core.ResultGenerator;
import com.ryenlii.datatrans.core.constant.TopicConst;
import com.ryenlii.datatrans.entity.HnOperationBoundary;
import com.ryenlii.datatrans.entity.Student;
import com.ryenlii.datatrans.mapper.tdengine.StudentMapper;
import com.ryenlii.datatrans.service.IProducerService;
import com.ryenlii.datatrans.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author zhengwen
 */

@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource(name = "opt")
    private IInfluxBaseService opt;

    @Resource
    private IProducerService producerService;

    private static final AtomicInteger atomicInteger = new AtomicInteger(1);

    public Result<?> add(Student record, String lang) {
        record.setCreateTime(DateUtil.dateSecond().toTimestamp());
        int inNum = studentMapper.insert(record);
        if (inNum != 1) {
            return ResultGenerator.genFailResult(ResponseCodeI18n.SAVE_FAIL.getMsg(), lang);
        }
        return ResultGenerator.genSuccessResult(record);
    }

    @Override
    public Result<?> update(Student record, String lang) {
        // 先查询
        Long id = record.getId();
        QueryWrapper<Student> qw = new QueryWrapper<>();
        qw.eq("id", id);
        Student dbStudent = baseMapper.selectOne(qw);
        // 存在就修改
        if (dbStudent != null) {
            BeanUtil.copyProperties(record, dbStudent, CopyOptions.create().setIgnoreNullValue(true));
        }
        // tdengine更新使用insert方式
        int upNum = baseMapper.insert(dbStudent);
        if (upNum != 1) {
            return ResultGenerator.genFailResult(ResponseCodeI18n.UPDATE_FAIL.getMsg(), lang);
        }
        // 不存在提示
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public IPage<Student> selectPage(Page page, Student customQueryParams) {
        // QueryWrapper<Student> qw = new QueryWrapper<>();
        // IPage<Student> records = baseMapper.selectPage(page, qw);
        IPage<Student> records = baseMapper.selectStudentPage(page, customQueryParams);
        return records;
    }

    @Override
    public Result<?> get(Long id) {
        log.info("id:{}", id);
        if (id == null) {
            id = 1L;
        }

        List<HnOperationBoundary> klist = new ArrayList<>();
        for (int i = 0; i < id; i++) {
            HnOperationBoundary var = new HnOperationBoundary();
            var.setOperationId(id);
            var.setId(IdUtil.getSnowflakeNextId());
            var.setPeriod(i / 2 == 0 ? 1 : 0);
            var.setLongitude(String.valueOf(114.457616D + i));
            var.setLatitude(String.valueOf(31.950721D + i));
            klist.add(var);
        }

        opt.saveToDb(klist);

        producerService.sendMessage(TopicConst.PLATFORM_SUBSCRIBE_CITY_DATA, JSONUtil.toJsonStr(klist));

        String sql = "select count(*) from td_opera_bound_" + id;
        // List<HnOperationBoundary> list2 = opt.select(sql, "", HnOperationBoundary.class);
        long list2 = opt.selectCountFromInfluxBySql(sql);
        log.info("list2:{}", list2);
        atomicInteger.addAndGet(1);

        klist.clear();

        return ResultGenerator.genSuccessResult();
    }
}
